﻿using Newtonsoft.Json;
using QingZhouLinYe_Web.DbContext;
using QingZhouLinYe_Web.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Web;

namespace QingZhouLinYe_Web.Ashxs
{
    /// <summary>
    /// getQuHuas 的摘要说明
    /// </summary>
    public class getQuHuas : IHttpHandler
    {
        private DbServices dbServices = new DbServices();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //List<QuHua> qhs = dbServices.getQuHuaTree();
            //String jsonstring = JsonConvert.SerializeObject(qhs);
            //context.Response.Write(BaseClass.JsonIndentation(jsonstring));

            async Task<QuHua> Test()
            {

                //List<QuHua> tasks = new List<QuHua>();
                QuHua tasks = new QuHua();

                var db = DbHelper.GetDb();//db需要是同一个
                db.BeginTran(); //强制关闭自动释放（如果手动释放可以不用事务）

                Debug.WriteLine("ssss");



                //如果是SqlSugarScope禁止出现Task.Run 
                //原因：有Task.Run SqlSugarScope异步上下文会变事务会有问题 
                tasks = await db.Queryable<QuHua>()
                    .SingleAsync();
                        //.ToListAsync();//直接等于ORM异步方法
                Debug.WriteLine("nnnnnn");
                //for (int i = 0; i < tasks.Count; i++)
                //{
                    Debug.WriteLine("ssss" + tasks.MingCheng);
                //}
                //await Task.WhenAll(tasks);
                db.CommitTran(); //关闭数据库
                return tasks;
            }

            QuHua qhs = Test();

            context.Response.Write(BaseClass.JsonIndentation(JsonConvert.SerializeObject(qhs)));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}